package com.bytz.modules.cms.productLine.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.system.base.controller.BytzController;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.productLine.entity.ProductLine;
import com.bytz.modules.cms.productLine.service.IAdminProductLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @BelongsProject: cms-backend
 * @BelongsPackage: com.bytz.modules.cms.productLine.controller
 * @Author: dst
 * @CreateTime: 2022-10-27  14:06
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@Api(tags = "生产线管理")
@RequestMapping("/productLine")
public class AdminProductLineController extends BytzController<ProductLine, IAdminProductLineService> {

    @Autowired
    private IAdminProductLineService productLineService;

    @ApiOperation(value = "生产线管理-分页查询", notes = "生产线管理-分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @PreAuthorize(value = "hasAuthority('" + AdminAuthConstants.ProductLine.EDIT + "')")
    public Result<IPage<ProductLine>> page(ProductLine productLine,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                           HttpServletRequest req) {

        return Result.ok(productLineService.page(
                new Page<>(pageNo, pageSize),
                QueryGenerator.initQueryWrapper(productLine, req.getParameterMap())
        ));
    }

    @ApiOperation(value = "查询全部", notes = "查询全部")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public Result<List<ProductLine>> page() {

        return Result.ok(productLineService.list());
    }

    @ApiOperation(value = "查询当前登录用户对应的产品线", notes = "查询当前登录用户对应的产品线")
    @RequestMapping(value = "/listByLoginUser", method = RequestMethod.GET)
    public Result<List<ProductLine>> listByLoginUser() {

        return Result.ok(productLineService.listByLoginUser());
    }

    @ApiOperation(value = "生产线管理-保存或编辑", notes = "生产线管理-保存或编辑")
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @PreAuthorize(value = "hasAuthority('" + AdminAuthConstants.ProductLine.EDIT + "')")
    public Result<ProductLine> saveOrUpdate(@RequestBody ProductLine productLine) {

        return Result.ok(productLineService.saveOrUpdateSaveObject(productLine, true));
    }

    @ApiOperation(value = "生产线管理-删除", notes = "生产线管理-删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize(value = "hasAuthority('" + AdminAuthConstants.ProductLine.EDIT + "')")
    public Result<Boolean> delete(@RequestParam String id) {

        return Result.ok(productLineService.checkAndRemoveById(id));
    }

}
